package com.TeamJigsaw.DanteTrail.Presenters;
import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JOptionPane;

import com.TeamJigsaw.DanteTrail.Debug.Debug;
import com.TeamJigsaw.DanteTrail.Models.Party;
import com.TeamJigsaw.DanteTrail.Models.RiverCheckpoint;
import com.TeamJigsaw.DanteTrail.Views.MainViewFrame;
import com.TeamJigsaw.DanteTrail.Views.Interfaces.RiverViewInterface;
/**
 * Represents a checkpoint for a River Checkpoint
 * @author Victor
 *
 */
public class RiverViewController {
	private Party party;
	private MainViewFrame mainView;
	private RiverViewInterface river;
	private GameViewController game;
	private RiverCheckpoint currRiver;
	private Random generator;
	private int percentWeight;
	
	/**
	 * @wbp.parser.entryPoint
	 */
	public RiverViewController(Party party, MainViewFrame mainView, GameViewController game, RiverCheckpoint currRiver){
		this.party = party;
		this.mainView = mainView;
		this.game = game;
		this.currRiver = currRiver;
		percentWeight = party.getWeight()/3500;
		generator = new Random();
		river = this.mainView.getRiverView();
		if (!river.listenersExist())
			createListeners();
		
	}
	/**
	 * Creates the listeners needed for the components in the view
	 */
	public void createListeners() {
		ActionListener nextListen = new NextButtonListener();
		river.setNextButtonListener(nextListen);
	}
	public RiverViewInterface getRiverView(){
		return river;
	}
	
	private class NextButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			int choice = river.getSelection();
			if (choice == 1){
				if (currRiver.isFerry()){
					if (party.getMoney() < 50){
						JOptionPane.showMessageDialog(mainView, "You don't have enough money to use the ferry!");
					}
					else {
						party.addMoney(-50);
						((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "Game");
						mainView.setTitle("Trail");
						GameViewController gvc = new GameViewController(party, mainView);

					}
				}
				else {
					JOptionPane.showMessageDialog(mainView, "This river does not have a ferry to use");
				}
			}
			else if (choice == 3){
				// TODO when the wagon flips, you must either have a person drown, or items lost.
				if (currRiver.getDepth() > 3){
					JOptionPane.showMessageDialog(mainView, "The River is too deep to Ford, try another option.");
				}
				else{
					int rand = generator.nextInt(101);
					rand = rand*(percentWeight+1);
					Debug.println("" + rand, Debug.M_NINE_DEMO);	
					if (rand > 88){
						rand = generator.nextInt(100);
						if (rand < 30){
							int maxFoodRemoved = (int)(party.getFood() * .1);
							double percentRemoved = rand / 30;
							int food = (int)(maxFoodRemoved * percentRemoved);
							party.addFood(-food);
							JOptionPane.showMessageDialog(mainView, "The wagon flipped, you lost " + food + " food.");
						}
						else if (rand < 60){
							int maxClothesRemoved = (int)(party.getClothes() * .1);
							double percentRemoved = rand / 60;
							int clothes = (int)(maxClothesRemoved * percentRemoved);
							party.addClothes(-clothes);
							JOptionPane.showMessageDialog(mainView, "The wagon flipped, you lost " + clothes + " clothes.");
						}
						else if (rand < 90){
							int maxAmmoRemoved = (int)(party.getAmmo() * .1);
							double percentRemoved = rand / 30;
							int ammo = (int)(maxAmmoRemoved * percentRemoved);
							party.addAmmo(-ammo);
							JOptionPane.showMessageDialog(mainView, "The wagon flipped, you lost " + ammo + " ammo.");
						}
						else if (rand < 99){
							boolean killed = false; 
							rand = generator.nextInt(3);
							while (!killed){
								if (party.getParty().get(rand).getStatus() == 0){
									party.getParty().get(rand).setStatus(1);
									killed = true;
								}
							}
							int sum = 0;
							for (int i = 0; i < 3; i++){
								if (party.getParty().get(i).getStatus() == 1){
									sum++;
								}
							}
							if (sum == 3){
								//TODO actually put this into the "Game Over" screen"
								JOptionPane.showMessageDialog(mainView, "Game Over! You party is all dead");
							}
							else {
								JOptionPane.showMessageDialog(mainView, party.getParty().get(rand).getName() + " has drowned when your wagon flipped.");
							}
						}
					}
					else {
						((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "Game");
						mainView.setTitle("Trail");
						GameViewController gvc = new GameViewController(party, mainView);
					}
				}
			}
			else if (choice == 2){
				RiverGameController rgc = new RiverGameController (party, mainView);
				((CardLayout) mainView.getContentPane().getLayout()).show(mainView.getContentPane(), "River Game");
				mainView.setTitle("River Game");
			}
		}
	}

}
